package com.yoyo.yyadsdk.config;

import android.annotation.SuppressLint;

import androidx.annotation.IntDef;

import com.anye.greendao.gen.LocalPullEntityDao;
import com.yoyo.yyadsdk.bean.dao.ConfigEntity;
import com.yoyo.yyadsdk.bean.dao.LocalPullEntity;
import com.yoyo.yyadsdk.util.GreenDaoManager;
import com.yoyo.yyadsdk.util.TimeUtils;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.List;

@SuppressLint("ShiftFlags")
public class PullFeature {

    /**
     * 两次拉活间隔活跃时间 单位：分钟
     */
    public static long pullFeatureInterval = -1;
    public static long adFeatureInterval = -1;
    public static long pullFeatureLastPullTime = -1;

    public static long getPullFeatureLastPullTime() {
        if (pullFeatureLastPullTime < 0) {
            List<LocalPullEntity> list = GreenDaoManager.getInstance().getDaoSession().getLocalPullEntityDao().queryBuilder()
                    .orderDesc(LocalPullEntityDao.Properties.PullTime).limit(1).list();
            if (list.size() > 0) {
                pullFeatureLastPullTime = list.get(0).getPullTime();
            } else {
                return 0;
            }
        }
        return pullFeatureLastPullTime;
    }

    public static void setPullFeatureLastPullTime(long pullFeatureLastPullTime) {
        PullFeature.pullFeatureLastPullTime = pullFeatureLastPullTime;
    }

    public static long getPullFeatureInterval() {
        if (pullFeatureInterval < 0) {
            ConfigEntity configEntity = Constants.getConfigEntity();
            if (configEntity == null) return 0;
            pullFeatureInterval = configEntity.getInterval();
        }
        return pullFeatureInterval * TimeUtils.SECOND;
    }

    public static void setPullFeatureInterval(long pullFeatureInterval) {
        PullFeature.pullFeatureInterval = pullFeatureInterval;
    }

    public static long getAdFeatureInterval() {
        if (adFeatureInterval < 0) {
            List<ConfigEntity> configEntities = GreenDaoManager.getInstance().getDaoSession().getConfigEntityDao().loadAll();
            if (configEntities.size() > 0) {
                ConfigEntity configEntity = configEntities.get(0);
                adFeatureInterval = configEntity.getAdInterval();
            } else {
                return 0;
            }
        }
        return adFeatureInterval * TimeUtils.SECOND;
    }

    public static void setAdFeatureInterval(long adFeatureInterval) {
        PullFeature.adFeatureInterval = adFeatureInterval;
    }

    /**
     * 屏幕状态
     */
    public static int SCREEN_STATE = 0;
    public static int SCREEN_STATE_ = 0;
    public static final int SCREEN_STATE_UNLOCK = 1;
    public static final int SCREEN_STATE_LOCK = 2;

    public static String SHORTCUT_CALLBACK_RECEIVER = "com.yoyo.yyadsdk.broad.ShortCutCallbackReceiver";

    @IntDef(flag = true, value = {
            PULL_LAUNCHER,
            PULL_OPEN,
            PULL_CLOSE,
            PULL_UNLOCK,
            PULL_AUTO,
            PULL_CLOSE_AUTO,
            PULL_KILL_APP,
            PULL_UN_KNOW
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface PullTypeFlags {
    }

    //启动拉活
    public static final int PULL_LAUNCHER = 0x01;
    //开屏拉活
    public static final int PULL_OPEN = 0x02;
    //息屏拉活
    public static final int PULL_CLOSE = 0x03;
    //锁屏拉活
    public static final int PULL_UNLOCK = 0x04;
    //定时拉活
    public static final int PULL_AUTO = 0x05;
    //灭屏后拉活
    public static final int PULL_CLOSE_AUTO = 0x06;
    //主动杀死应用进程
    public static final int PULL_KILL_APP = 0x07;
    //未知拉活
    public static final int PULL_UN_KNOW = 0x08;


    @IntDef(flag = true, value = {
            ADV_LOCK_SCREEN,
            ADV_BANNER_UP,
            ADV_BANNER_DOWN,
            ADV_ICON_UP,
            ADV_ICON_DOWN,
            ADV_TABLE_PLAQUE,
            ADV_APP_TABLE_PLAQUE,
            ADV_SPLASH,
            ADV_BANNER_UP_,
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface AdvTypeFlags {
    }

    public static final int ADV_LOCK_SCREEN = 0x01;
    public static final int ADV_BANNER_UP = 0x02;
    public static final int ADV_BANNER_DOWN = 0x03;
    public static final int ADV_ICON_UP = 0x04;
    public static final int ADV_ICON_DOWN = 0x05;
    public static final int ADV_TABLE_PLAQUE = 0x06;
    public static final int ADV_APP_TABLE_PLAQUE = 0x07;
    public static final int ADV_SPLASH = 0x08;
    public static final int ADV_BANNER_UP_ = 0x09;

    @IntDef(flag = true, value = {
            PULL_WHITE_LIST,
            PULL_BLACK_LIST
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface RuleListFlags {
    }

    public static final int PULL_WHITE_LIST = 0x00;
    public static final int PULL_BLACK_LIST = 0x01;


    @IntDef(flag = true, value = {
            PULL_MONITOR_TIME_ALIVE,
            PULL_MONITOR_TIME_KILL
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface MonitorTimeFlags {
    }

    public static final int PULL_MONITOR_TIME_ALIVE = 0x01;
    public static final int PULL_MONITOR_TIME_KILL = 0x02;


    @IntDef(flag = true, value = {
            USE_STATUS_ENABLE,
            USE_STATUS_DISABLE
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface UseStatusFlage {
    }

    public static final int USE_STATUS_ENABLE = 0x01;
    public static final int USE_STATUS_DISABLE = 0x02;

    @IntDef(flag = true, value = {
            ALARM_MONITOR_TIME_AUTO_PULL,
            ALARM_MONITOR_TIME_ALIVE,
            ALARM_MONITOR_TIME_KILL,
            ALARM_UPDATE_DATA,
            ALARM_SHORT_CUT,
            ALARM_DEL_SHORT_CUT
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface AlarmFlage {
    }

    public static final int ALARM_MONITOR_TIME_AUTO_PULL = 0x01;
    public static final int ALARM_MONITOR_TIME_KILL = 0x02;
    public static final int ALARM_UPDATE_DATA = 0x03;
    public static final int ALARM_SHORT_CUT = 0x04;
    public static final int ALARM_DEL_SHORT_CUT = 0x06;
    public static final int ALARM_MONITOR_TIME_ALIVE = 0x05;
}
